﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_VBPQ_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub ShowData()
        Using data As New VSEntities
            'Show loai van ban
            Dim lvb = (From ck In data.VBPQLoaiVanBans Select ck).ToList
            If Not lvb Is Nothing Then
                drpType.DataSource = lvb
                drpType.DataTextField = "LoaiVanBan"
                drpType.DataValueField = "Id"
                drpType.DataBind()
            End If
            'Show co quan ban hanh da ton tai
            Dim cqbh = (From ck In data.VBPQCoQuanBHs Select ck).ToList
            If Not cqbh Is Nothing Then
                lsbExistingPromulgator.DataSource = cqbh
                lsbExistingPromulgator.DataTextField = "CoQuanBH"
                lsbExistingPromulgator.DataValueField = "Id"
                lsbExistingPromulgator.DataBind()

            End If
            'Show nguoi ky da ton tai                
            Dim nk = (From ck In data.VBPQNguoiKies Select ck).ToList
            If Not nk Is Nothing Then
                lsbExistingSigner.DataSource = nk
                lsbExistingSigner.DataTextField = "NguoiKy"
                lsbExistingSigner.DataValueField = "Id"
                lsbExistingSigner.DataBind()
            End If
            'Show chuc danh da ton tai
            Dim cd = (From ck In data.VBPQChucDanhs Select ck).ToList
            If Not cd Is Nothing Then
                lsbExistingOffice.DataSource = cd
                lsbExistingOffice.DataTextField = "ChucDanh"
                lsbExistingOffice.DataValueField = "Id"
                lsbExistingOffice.DataBind()
            End If
        End Using
    End Sub
    Public Sub Move(ByVal SourceList As ListBox, ByVal DestList As ListBox)
        Dim lstSelected = SourceList.GetSelectedIndices()
        Dim i As Integer = lstSelected.Count
        Dim size As Integer = SourceList.Items.Count()
        While i > 0
            i = i - 1
            Dim Indexed As Integer = lstSelected(i)
            DestList.Items.Add(SourceList.Items(Indexed))
            SourceList.Items.RemoveAt(Indexed)

        End While
        SourceList.SelectedValue = Nothing
        DestList.SelectedValue = Nothing
    End Sub
#End Region
#Region "Event for control"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "thupv", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "thupv", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            ShowData()
        End If
    End Sub

    Protected Sub btnMoveCQBH_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveCQBH.Click
        Move(lsbExistingPromulgator, lsbPromulgator)
    End Sub
    Protected Sub btnMoveSigner_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveSigner.Click
        Move(lsbExistingSigner, lsbSigner)
    End Sub
    Protected Sub btnMoveBackCQBH_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveBackCQBH.Click
        Move(lsbPromulgator, lsbExistingPromulgator)
    End Sub
    Protected Sub btnMoveBackSigner_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveBackSigner.Click
        Move(lsbSigner, lsbExistingSigner)
    End Sub
    Protected Sub btnMoveOffice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveOffice.Click
        Move(lsbExistingOffice, lsbOffice)
    End Sub
    Protected Sub btnMoveBackOffice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveBackOffice.Click
        Move(lsbOffice, lsbExistingOffice)
    End Sub

    Protected Sub hplInsertCQBH_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplInsertCQBH.Init
        hplInsertCQBH.CssClass = "thickbox"
        hplInsertCQBH.NavigateUrl = "~/Page/VBPQPromulgator/List.aspx?keepThis=true&TB_iframe=true&height=450&width=400&modal=true"
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx?Id=" & Request("Id"))
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As New VBPQDocument
            Try
                p.TrichYeu = txtTitle.Text.Trim()
                p.SoKyHieu = txtCode.Text.Trim()
                p.LoaiVanBanId = drpType.SelectedValue
                p.StatusId = 1
                p.NgayBanHanh = IIf(String.IsNullOrEmpty(txtPublishedDate.Text.ToString), Nothing, StringToDate(txtPublishedDate.Text.Trim()))
                p.NgayDangCongBao = IIf(String.IsNullOrEmpty(txtPrintedDate.Text.ToString), Nothing, StringToDate(txtPrintedDate.Text.Trim()))
                p.NgayCoHieuLuc = IIf(String.IsNullOrEmpty(txtValidatedDate.Text.ToString), Nothing, StringToDate(txtValidatedDate.Text.Trim()))
                p.NgayHetHieuLuc = IIf(String.IsNullOrEmpty(txtExpiredDate.Text.ToString), Nothing, StringToDate(txtExpiredDate.Text.Trim()))
                p.NguonTrich = txtPublisher.Text.Trim()
                p.NoiDung = txtContent.Text.Trim()
                p.FileAttach = hidImagePath.Value
                data.VBPQDocuments.AddObject(p)
                data.SaveChanges()
                Dim _Documentid = p.Id
                ' Save VBPQCoQuanBanHanhDocument
                For i As Integer = 0 To lsbPromulgator.Items.Count - 1
                    Dim cqbh As New VBPQCoQuanBanHanhDocument
                    cqbh.DocumentId = _Documentid
                    cqbh.CoQuanBanHanh = lsbPromulgator.Items(i).Value
                    data.VBPQCoQuanBanHanhDocuments.AddObject(cqbh)
                Next
                ' Luu vao bang VBPQChucDanhsNguoiKies
                If lsbSigner.Items.Count = lsbOffice.Items.Count Then 'so luong nguoi ky duoc chon bang voi so luong chuc danh
                    For i As Integer = 0 To lsbSigner.Items.Count - 1
                        For j As Integer = i To lsbOffice.Items.Count - 1
                            Dim nk As New VBPQChucDanhNguoiKy
                            nk.DocumentId = p.Id
                            nk.NguoiKyId = lsbSigner.Items(i).Value
                            nk.ChucDanhId = lsbOffice.Items(j).Value
                            data.VBPQChucDanhNguoiKies.AddObject(nk)
                            Exit For
                        Next
                    Next
                ElseIf lsbSigner.Items.Count < lsbOffice.Items.Count Then 'so luong nguoi ky duoc chon it hon so luong chuc danh
                    For i As Integer = 0 To lsbSigner.Items.Count - 1
                        For j As Integer = 0 To lsbOffice.Items.Count - 1
                            Dim nk As New VBPQChucDanhNguoiKy
                            nk.DocumentId = p.Id
                            nk.NguoiKyId = lsbSigner.Items(i).Value
                            nk.ChucDanhId = lsbOffice.Items(j).Value
                            data.VBPQChucDanhNguoiKies.AddObject(nk)
                        Next
                    Next
                End If
                data.SaveChanges()
                'Insert_App_Log("Insert Candidate:" & txtTitle.Text.Trim & "", Function_Name.Candidate, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/VBPQ/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub hplInsertSigner_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplInsertSigner.Init
        hplInsertSigner.CssClass = "thickbox"
        hplInsertSigner.NavigateUrl = "~/Page/VBPQSigner/List.aspx?keepThis=true&TB_iframe=true&height=450&width=400&modal=true"
    End Sub
    Protected Sub hplInsertOffice_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplInsertOffice.Init
        hplInsertOffice.CssClass = "thickbox"
        hplInsertOffice.NavigateUrl = "~/Page/VBPQOffice/List.aspx?keepThis=true&TB_iframe=true&height=450&width=400&modal=true"
    End Sub
    Protected Sub btnReloadList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReloadList.Click
        Using data As New VSEntities
            'Show loai van ban
            Dim lvb = (From ck In data.VBPQLoaiVanBans Select ck).ToList
            If Not lvb Is Nothing Then
                drpType.DataSource = lvb
                drpType.DataTextField = "LoaiVanBan"
                drpType.DataValueField = "Id"
                drpType.DataBind()
            End If
            'Show co quan ban hanh da ton tai
            Dim cqbh = (From ck In data.VBPQCoQuanBHs Select ck).ToList
            If Not cqbh Is Nothing Then
                lsbExistingPromulgator.DataSource = cqbh
                lsbExistingPromulgator.DataTextField = "CoQuanBH"
                lsbExistingPromulgator.DataValueField = "Id"
                lsbExistingPromulgator.DataBind()

            End If
            'Show nguoi ky da ton tai                
            Dim nk = (From ck In data.VBPQNguoiKies Select ck).ToList
            If Not nk Is Nothing Then
                lsbExistingSigner.DataSource = nk
                lsbExistingSigner.DataTextField = "NguoiKy"
                lsbExistingSigner.DataValueField = "Id"
                lsbExistingSigner.DataBind()
            End If
            'Show chuc danh da ton tai
            Dim cd = (From ck In data.VBPQChucDanhs Select ck).ToList
            If Not cd Is Nothing Then
                lsbExistingOffice.DataSource = cd
                lsbExistingOffice.DataTextField = "ChucDanh"
                lsbExistingOffice.DataValueField = "Id"
                lsbExistingOffice.DataBind()
            End If
        End Using
    End Sub
    Protected Sub hplTypeInsert_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplTypeInsert.Init
        hplTypeInsert.CssClass = "thickbox"
        hplTypeInsert.NavigateUrl = "~/Page/VBPQType/List.aspx?keepThis=true&TB_iframe=true&height=450&width=400&modal=true"
    End Sub
#End Region
End Class